package com.googlecode.imanager.swing.form;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.text.JTextComponent;

import com.googlecode.imanager.client.bridge.form.IInputText;
import com.googlecode.imanager.client.form.InputText;
import com.googlecode.imanager.client.util.Utils;
import com.googlecode.imanager.core.base.BridgeApplication;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public abstract class SwingInputText extends SwingInputControl implements IInputText {

    public SwingInputText(BridgeApplication bridge, InputText source) {
        super(bridge, source);
    }

    @Override
    protected abstract JTextComponent createTarget();

    @Override
    public JTextComponent getTarget(){
        return (JTextComponent) target;
    }
    
    @Override
    public InputText getSource() {
        return (InputText) source;
    }

    @Override
    protected void doAttachEvents() {
        super.doAttachEvents();
        getTarget().addKeyListener(new KeyAdapter() {
            
            @Override
            public void keyReleased(KeyEvent e) {
                synchronized (TREE_LOCK) {
                    if (!isDropped()){
                        startFireUIEvent();
                        try {
                            getSource().setText(getTarget().getText());
                        } catch (Exception ex) {
                            processException(ex);
                        } finally {
                            endFireUIEvent();
                        }
                    }
                }
            }            
        });
    }

    @Override
    public void setAccessKey(Character accessKey) {
        if (accessKey == null){
            getTarget().setFocusAccelerator('\0');
        } else {
            getTarget().setFocusAccelerator(accessKey);
        }
    }

    @Override
    public int getSelectionEnd() { 
        return getTarget().getSelectionEnd(); 
    }

    @Override
    public int getSelectionStart() {
        return getTarget().getSelectionStart(); 
    }

    @Override
    public void setSelection(int selectionStart, int selectionEnd) {
        getTarget().setSelectionStart(selectionStart);
        getTarget().setSelectionEnd(selectionEnd);
    }

    @Override
    public int getCaretPosition() {
        return getTarget().getCaretPosition();
    }

    @Override
    public void setCaretPosition(int caretPosition) {
        if (caretPosition < 0){
            caretPosition = 0;
        } else {
            String txt = getTarget().getText();
            int max = txt == null ? 0 : txt.length();
            if (caretPosition > max){
                caretPosition = max;
            }
        }
        getTarget().setCaretPosition(caretPosition);
    }

    @Override
    public void setText(String text) {
        if (!Utils.isEquals(text, getTarget().getText())){
            int pos = getCaretPosition();
            getTarget().setText(text);
            setCaretPosition(pos);
        }
    }

}
